//
//  BaseNaviVC.swift
//  TestDemoSwift
//
//  Created by 王锦发 on 2020/3/29.
//  Copyright © 2020 WJF. All rights reserved.
//

import UIKit
import EachNavigationBar

class BaseNaviVC: UINavigationController {
    
    @available(iOS 13.0, *)
    override var overrideUserInterfaceStyle: UIUserInterfaceStyle {
        get { return topViewController?.overrideUserInterfaceStyle ?? .light }
        set {  }
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return topViewController?.supportedInterfaceOrientations ?? .all
    }
    
    override var shouldAutorotate: Bool {
        return topViewController?.shouldAutorotate ?? true
    }
    
    /// 修改导航栏返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool = true) {
        if self.viewControllers.count > 0 {
            let leftItem = UIBarButtonItem(image: .ArrowFork.icon_leftArrow, style: .done, target: self, action: #selector(back))
            viewController.navigation.item.leftBarButtonItem = leftItem
            if self.viewControllers.count == 1 {
                viewController.hidesBottomBarWhenPushed = true
            }
        } else {
            viewController.hidesBottomBarWhenPushed = false
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func back() {
        self.popViewController(animated: true)
    }
    
    override var childForStatusBarStyle: UIViewController? {
        /**
         自定义UINavigationController，需要重写childForStatusBarStyle。
         否则preferredStatusBarStyle不执行。
         */
        return topViewController
    }
    
    // MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.interactivePopGestureRecognizer?.delegate = self
        
    }
    
}

// MARK: - UIGestureRecognizerDelegate代理
extension UINavigationController: UIGestureRecognizerDelegate {
    
    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if children.count == 1 {
            return false
        } else {
            return true
        }
    }
    
    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        /******处理侧滑手势与scrollview手势冲突*******/
        return gestureRecognizer is UIScreenEdgePanGestureRecognizer
    }
    
}

// MARK: - 导航栏配置
extension BaseNaviVC {
    
    static func setNaviBarAppearence(navi: inout BaseNaviVC) {
        navi.navigation.configuration.isEnabled = true
        navi.navigation.configuration.barTintColor = .lightWhiteDark27
        navi.navigation.configuration.tintColor = .lightBlackDarkWhite
        navi.navigation.configuration.isTranslucent = false
        navi.navigation.configuration.titleTextAttributes = [
            .foregroundColor: UIColor.lightBlackDarkWhite,
            .font: UIFont.systemFont(ofSize: 16, weight: .bold)
        ]
//        let shadow = Shadow(color: UIColor.lightBlackDarkWhite.cgColor, opacity: 0.05, offset: CGSize(width: 0, height: 0.5))
        navi.navigation.configuration.shadow = Shadow.none
        navi.navigation.configuration.isShadowHidden = true
        navi.navigation.configuration.backItem = .init(style: .image(.ArrowFork.icon_leftArrow))
    }
    
}
